/*
编写 : Hexh
时间 : 2012-7-9 10:48:31
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsGateinfo
	/// </summary>
	public class FsGateinfoController : BaseController
	{

		private readonly IFsGateinfoFacade FsGateinfoFacade;
		private readonly IHospUserFacade HospUserFacade;
		private readonly IDepartmentFacade DepartmentFacade;

		public FsGateinfoController(IFsGateinfoFacade FsGateinfoFacade, IHospUserFacade HospUserFacade, IDepartmentFacade DepartmentFacade)
		{
			this.FsGateinfoFacade = FsGateinfoFacade;
			this.HospUserFacade = HospUserFacade;
			this.DepartmentFacade = DepartmentFacade;
		}

		[Description("访问FsGateinfo管理页面")]
		[ViewPage]
		public ActionResult FsGateinfo()
		{
			return View();
		}

		[ExtResult]
		[Description("添加修改FsGateinfo信息")]
		public ActionResult SaveOrUpdate()
		{
			FsGateinfo entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsGateinfo
				{
					Name = Request["Name"],
					Code = Request["Code"],
					Type = Request["Type"],
					Ip = Request["Ip"],
					Mac = Request["Mac"],
					InAreaId = Request["InAreaId"],
					OutAreaId = Request["OutAreaId"],
					State = Request["State"],
					Description = Request["Description"],
				};
			}
			else
			{
				// get the entity.
				entity = FsGateinfoFacade.Get(Request["Id"]);
				// set new value..
				entity.Name = Request["Name"];
				entity.Code = Request["Code"];
				entity.Type = Request["Type"];
				entity.Ip = Request["Ip"];
				entity.Mac = Request["Mac"];				
				entity.InAreaId = Request["InAreaId"];
				entity.OutAreaId = Request["OutAreaId"];
				entity.State = Request["State"];
				entity.Description = Request["Description"];
			}
			// 保存信息
			FsGateinfoFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("删除FsGateinfo基本资料")]
		public ActionResult Delete()
		{
			FsGateinfoFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("物理删除FsGateinfo基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsGateinfoFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsGateinfo清单")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsGateinfoFacade.GetPlist(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsGateinfo> { total = total, data = data },
				new string[] { "" });
		}

		[Description("获取FsGateinfo信息")]
		public ActionResult GetById()
		{
			var entity = FsGateinfoFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsGateinfo> { data = new FsGateinfo[] { entity } }, new string[] { "Departments" }
			   );
		}

		[ExtResult]
		[Description("判断FsGateinfo字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsGateinfoFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
